<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
    <style>
        .aui-searchbar-input {
            width: 98% !important;
        }
    </style>
</head>

<body>
    <header id='header' class="aui-bar aui-bar-nav" style="background-color: #03a9f4;position:fixed;">
        <a class="aui-pull-left aui-btn" onclick="api.closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="middle" style="font-size: 0.8rem;">
            <span>APP盘点详细</span><span id="titleVal"></span>
        </div>
    </header>
    <div class="aui-content aui-margin-b-15" style="margin-top:0px;">


    </div>
    <ul class="ui-selectee" style="margin-top:60px;">

        <li class='sortable ui-selectee ui-sortable'>
            <ul class='aui-list aui-media-list ui-selectee'>
                <li class='aui-list-item ui-selectee'>
                    <div class='aui-media-list-item-inner ui-selectee'>
                        <div class='aui-list-item-inner ui-selectee'>
                            <div class='aui-list-item-text ui-selectee' style='height: 22px;'>
                                <div class='aui-list-item-label ui-selectee list-tr-title'>盘点单号</div>
                                <div class='aui-list-item-inner ui-selectee' id="pdCode"></div>
                            </div>
                            <div class='aui-list-item-text ui-selectee' style='height: 22px;'>
                                <div class='aui-list-item-label ui-selectee list-tr-title'>盘点任务号</div>
                                <div class='aui-list-item-inner ui-selectee' id="pdrwCode"></div>
                            </div>
                            <div class='aui-list-item-text ui-selectee' style='height: 22px;'>
                                <div class='aui-list-item-label ui-selectee list-tr-title'>仓库名称</div>
                                <div class='aui-list-item-inner ui-selectee' id="workCenterName"></div>
                            </div>
                            <div class='aui-list-item-text ui-selectee' style='height: 22px;'>
                                <div class='aui-list-item-label ui-selectee list-tr-title'>库位名称</div>
                                <div class='aui-list-item-inner ui-selectee' id="workCellName"></div>
                            </div>
                        </div>
                    </div>
                </li>
            </ul>
        </li>

    </ul>
    <ul class="aui-list aui-form-list">
        <li class="aui-list-item">
            <div class="aui-list-item-inner">
                <div class="aui-list-item-label">器具条码</div>
                <div class="aui-list-item-input">
                    <input type="text" disabled="true" id="codeBar" value="" onclick="getIdSelector();" />
                </div>
                <div class="aui-btn aui-btn-primary" style="margin-right: 0.5rem;background-color: #03a9f4"
                    onclick="doSearch()">寻卡</div>
            </div>
        </li>
    </ul>
    <div style="width:100%;margin-top:2%;"></div>
    <div class="aui-btn aui-btn-info" style="width:90%;margin-left:5%;" onclick="save()">盘点确认</div>
    <!-- <div class="aui-btn aui-btn-info" style="margin-left:8%;width:40%;;" onclick="deleteData()">离线保存</div> -->

    <div style="background-color:white; height: 100%;width: 100%;margin-top:2%;margin-bottom:50px;">
        <table class="table" style='width:100%;' id="table">
            <thead>
                <tr
                    style="text-align: center;background-color: #DCE9F2;color: #467aa7;font-size: 12px;font-weight: bold;word-wrap:break-word;word-break:break-all;">
                    <td algin='center' width='80%'>器具条码</td>
                    <td algin='center' width='20%'>操作</td>
                </tr>
            </thead>
            <tbody id="tbodyHtml">
            </tbody>
        </table>
    </div>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript">
    var listData;
    var idSelector = "";//光标所在id
    var scanRule = ""; //条码解析规则

    var workCellGid = "";
    var workCellCode = "";
    var workCellName = "";

    //初始化查询仓库下拉框
    apiready = function () {
        $("#codeBar").focus();
        idSelector = "codeBar";
        listData = [];
        $api.html($api.byId("pdCode"), api.pageParam.code);
        $api.html($api.byId("pdrwCode"), api.pageParam.inventoryTaskCode);
        $api.html($api.byId("workCenterName"), api.pageParam.workCenterName);
        $api.html($api.byId("workCellName"), api.pageParam.workCellName);

    }

    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        idSelector = el.id;
        $("#" + el.id).select();
    }

    //扫描
    function doScan() {
        var moduleScanner = api.require('moduleScanner');
        var params = {
            top: 150, //扫描窗口的上边距
            width: 250, //扫描窗口的宽
            height: 250, //扫描窗口的高
            useColor: false, //是否使用自己设定的颜色(startColor和endColor),
            //如果不使用设定的颜色就使用默认的那张网格图
            startColor: '#0F08C8FF', //渐变起始颜色
            endColor: '#7F08C8FF', //渐变结束颜色
            animTime: 2200, //扫描动画时间,单位为毫秒
        };
        moduleScanner.toScannerActivityForResult(params, function (ret, err) {
            if (idSelector == 'cellCode') {

                $("#cellCode").val(ret);
                getWorkCell(ret, "cellCode");
            } else if (idSelector == 'codeBar') {

                tmSplit(ret);

            }
        });
    }

    //3；PDA自带扫描回车事件
    $("#codeBar").on('keypress', function (e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var sm = $(this).val();
        if (keycode == '13') {
            //仓库编码
            if (sm != null && sm != "" && sm != undefined) {
                $("#codeBar").blur();

                var codeBarCells = document.querySelectorAll("td[name='codeBar']");
                var codeBarArray = [];
                // 遍历这些<td>元素并提取它们的文本值
                codeBarCells.forEach(function (cell) {
                    codeBarArray.push(cell.innerText.trim());
                });

                if (!codeBarArray.includes(sm)) {
                    var html = "";
                    html += "<tr style='text-align: center;color: black;font-size: 12px;background-color: #F5F5F5;'>";
                    html += "<td name='codeBar'>" + sm + "</td>";
                    html += "<td name='button'>" + "<div class='aui-btn aui-btn-info' onclick='deleteRow(this)'>删除</div> " + "</td>";
                    html += "</tr>";
                    $("#tbodyHtml").append(html);
                }
                $("#codeBar").val("");
                $("#codeBar").focus();
                //$api.html($api.byId("tbodyHtml"), html);
            }
        }
    });

    function deleteRow(button) {
        var row = button.parentNode.parentNode;
        // 获取表格的引用
        var table = document.getElementById("table").getElementsByTagName('tbody')[0];
        // 删除当前行
        table.removeChild(row);
    }


    function save() {


        var codeBarCells = document.querySelectorAll("td[name='codeBar']");
        var codeBarArray = [];
        // 遍历这些<td>元素并提取它们的文本值
        codeBarCells.forEach(function (cell) {
            codeBarArray.push(cell.innerText.trim());
        });
        // 将提取的值合并成一个字符串
        var codeBar = codeBarArray.join(",");

        api.showProgress({
            title: '保存中...',
        });

        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/wminventorybillDetailSave";
        var data = {
            values: {
                un: un,
                appVersion: api.appVersion,
                sysid: api.pageParam.sysid,
                workCellId: api.pageParam.workCellId,
                workCellCode: api.pageParam.workCellCode,
                workCellName: api.pageParam.workCellName,
                codeBar: codeBar
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {

                    api.hideProgress();
                    api.toast({
                        msg: "保存成功",
                        duration: 3000
                    });
                    var html = "";
                    $("#tbodyHtml").append(html);
                } else if (myobj.code == '1') {
                    $("#codeBar").val("");
                    api.hideProgress();
                    $("#codeBar").select();
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });

        api.hideProgress();
    }

    //调用RFID扫码api
    function doSearch() {
        var moduleUhfRfid = api.require('moduleUhfRfid');
        moduleUhfRfid.startActivityForResult({
            appParam: ""
        }, function (ret, err) {
            if (ret) {
                // alert("start:"+ret);

                var list = eval(ret);
                console.log(list);
                var html = "";
                if (list != null && list != undefined && list.length > 0) {
                        //divStr += "<tr style='text-align:center;color:#0099FF;border-collapse: collapse'><th style='border: 1px solid white;border-collapse: collapsel;width:60%'>器具条码</th><th style='border: 1px solid white;border-collapse: collapse;width:40%'>删除条码</th></tr>"
                        for (var i = 0; i < list.length; i++) {

                            //divStr += "<tr style='text-align:center;color:#0099FF;border-collapse: collapse'><td id= 'tm' style='border: 1px solid white;border-collapse: collapse;width:60%''>" + list[i].ASCII + "</td>" + "<td style='border: 1px solid white;border-collapse: collapse;width:40%'><button class='deleteRowBtn' style = 'border:none:outline:none;background-color:red'>删除</button></td></tr>"
                         
                            html += "<tr style='text-align: center;color: black;font-size: 12px;background-color: #F5F5F5;'>";
                            html += "<td name='codeBar'>" +  list[i].ASCII  + "</td>";
                            html += "<td name='button'>" + "<div class='aui-btn aui-btn-info' onclick='deleteRow(this)'>删除</div> " + "</td>";
                            html += "</tr>";
                        }
                        // $("$table").append(divStr);
                        $("#tbodyHtml").append(html);
                    }
                
                $("#codeBar").val("");
                $("#codeBar").focus();
            } else {
                // alert("error:"+err);
            }
        });
    }

    //返回按钮
    function closeWin() {
        api.closeWin({});
    }
</script>

</html>